// components/timeSelect/timeSelect.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {

    showTimeText: {
      type: String,
      default: "",
    },
    styleType:{
      type:String,
      value:'black'
    }
  },
  observers: {
    'showTimeText': function (newValue) {
      if(newValue && newValue != ''){
        this.setData({
          showTime: newValue
        })
      }
     

    },
  },  

  lifetimes: {
    attached: function () {
      // 在组件实例进入页面节点树时执行
      setTimeout(() => {
        this.setData({
          delayCalendarShow: true
        })
      }, 1500);
      // this.startTimeInit();
    },
    detached: function () {
      // 在组件实例被从页面节点树移除时执行
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    delayCalendarShow: false,
    maxDate: new Date(2025, 12, 31).getTime(),
    bigMaxDate: new Date(2050, 12, 31).getTime(),
    minDate: new Date(2024, 2, 1).getTime(),
    defaultDate:[new Date().getTime(),new Date().getTime()],
    showTime: "选择时间段筛选",
    showTimePop: false,
    startTimeStr: "",
    endTimeStr: "",
  },

  /**
   * 组件的方法列表
   */
  methods: {
    selectTimeAction() {
      this.setData({
        showTimePop: true,
      })
    },
    onCloseCal() {
      this.setData({
        showTimePop: false,
      })
    },
    //天确认选择:
    onConfirmCal(e) {
      // console.log("JJLog:e:" + JSON.stringify(e));
      let timeArr = e.detail;
      this.timeDealMethod(timeArr);
    },
    //时间处理方法
    timeDealMethod(timeArr) {
      let startDate = timeArr[0];
      let endDate = timeArr[1];
      let startTime = "";
      let endTime = "";
      let resultText = "";
      let startMonth = (startDate.getMonth() + 1) > 9 ? (startDate.getMonth() + 1) : ("0" + (startDate.getMonth() + 1));
      let startDay = (startDate.getDate() + 1) > 9 ? (startDate.getDate()) : ("0" + startDate.getDate());
      let endMonth = (endDate.getMonth() + 1) > 9 ? (endDate.getMonth() + 1) : ("0" + (endDate.getMonth() + 1));
      let endDay = (endDate.getDate() + 1) > 9 ? (endDate.getDate()) : ("0" + endDate.getDate());
      startTime = startDate.getFullYear() + '-' + startMonth + '-' + startDay;
      endTime = endDate.getFullYear() + '-' + endMonth + '-' + endDay;
      this.setData({
        showTimePop: false,
        startTimeStr: startTime + " " + "00:00:00",
        endTimeStr: endTime + " " + "23:59:59"
      })

      if (startTime == endTime) {
        resultText = startTime
      } else {
        resultText = startTime + " - " + endTime
      }
      this.setData({
        showTime: resultText,
      })
      this.triggerEvent('timeConfirmAction', { showTimeText: resultText, startTimeStr: this.data.startTimeStr, endTimeStr: this.data.endTimeStr })

    },
    // 清空时间筛选:
    emptyTimeAction() {
      this.setData({
        showTime: "选择时间段筛选",
        showTimePop: false,
        startTimeStr: "",
        endTimeStr: ""
      })
      this.triggerEvent('timeConfirmAction', { showTimeText: this.data.showTime, startTimeStr: this.data.startTimeStr, endTimeStr: this.data.endTimeStr })

    },
    //初始数据:
    startTimeInit() {
      let d = new Date();
      let str = d.getFullYear().toString() + "-" + this.addZero(d.getMonth() + 1) + "-" + this.addZero(d.getDate())
      console.log(str);
      let startTime = str + " " + "00:00:00";
      let endTime = str + " " + "23:59:59";
      this.setData({
        startTimeStr: startTime,
        endTimeStr: endTime,
        showTime: str
      })
      // this.queryRecommendStatistics();
    },
    addZero(v) {
      if (v < 10)
        return '0' + v;
      return v.toString();
    },
  }
})
